package com.beamfield.controller.fore;

import com.beamfield.common.ApiResult;
import com.beamfield.common.PageResult;
import com.beamfield.controller.common.BaseController;
import com.beamfield.model.BeamContentWithBLOBs;
import com.beamfield.modelVo.CarouselVo;
import com.beamfield.modelVo.NewsAbbrVo;
import com.beamfield.modelVo.NewsVo;
import com.beamfield.modelVo.ProjectVo;
import com.beamfield.service.manage.portal.BeamContentService;
import com.beamfield.service.manage.portal.CarouseService;
import com.beamfield.service.manage.portal.NewsSectionService;
import com.beamfield.service.manage.portal.NewsService;
import com.beamfield.service.manage.sys.ProjectService;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 前台首页
 */
@Controller
@RequestMapping(value = "/fore")
public class IndexController extends BaseController{
    private final CarouseService carouseService;
    private final NewsSectionService newsSectionService;
    private final ProjectService projectService;
    private final BeamContentService beamContentService;
    private final NewsService newsService;

    @Autowired
    public IndexController(CarouseService carouseService, NewsSectionService newsSectionService, ProjectService projectService, BeamContentService beamContentService, NewsService newsService) {
        this.carouseService = carouseService;
        this.newsSectionService = newsSectionService;
        this.projectService = projectService;
        this.beamContentService = beamContentService;
        this.newsService = newsService;
    }

    @RequestMapping(value = {"/index",""}, method = RequestMethod.GET)
    public ModelAndView index(HttpServletRequest request){
        Long beamId = super.getBeamId(request);
        String rBeamId = request.getParameter("beamId");
        if(StringUtils.isNotEmpty(rBeamId) && NumberUtils.isDigits(rBeamId)){
            beamId = Long.parseLong(rBeamId);
        }
        ModelAndView mv = new ModelAndView();
        if(beamId == null || beamId == 0L){
            mv.setViewName("redirect:/cms/login");
            return mv;
        }
        return mv;
    }

    @RequestMapping(value = "/news/detail", method = RequestMethod.GET)
    public ModelAndView newsDetail(){
        return new ModelAndView();
    }

    @RequestMapping(value = "/news/list", method = RequestMethod.GET)
    public ModelAndView newsList(){
        return new ModelAndView();
    }

    @RequestMapping(value = "/beamContent", method = RequestMethod.GET)
    public ModelAndView beamContent(){
        return new ModelAndView();
    }

    /**
     * 获取轮播图
     * @param request
     * @return
     */
    @RequestMapping(value = "/carouse",method = RequestMethod.GET)
    @ResponseBody
    public ApiResult readCarouseData(HttpServletRequest request){
        Long beamId = super.getBeamId(request);
        String rBeamId = request.getParameter("beamId");
        if(StringUtils.isNotEmpty(rBeamId) && NumberUtils.isDigits(rBeamId)){
            beamId = Long.parseLong(rBeamId);
        }
        List<CarouselVo> carouselList = carouseService.getCarouseData(null, null, beamId);
        return new ApiResult(carouselList);
    }

    /**
     * 获取新闻缩略
     * @param request
     * @return
     */
    @RequestMapping(value = "/newsAbbr",method = RequestMethod.GET)
    @ResponseBody
    public ApiResult readNewsSection(HttpServletRequest request){
        Long beamId = super.getBeamId(request);
        String rBeamId = request.getParameter("beamId");
        if(StringUtils.isNotEmpty(rBeamId) && NumberUtils.isDigits(rBeamId)){
            beamId = Long.parseLong(rBeamId);
        }
        List<NewsAbbrVo> newsAbbrVoList = newsSectionService.getNewsAbbr(beamId);
        return new ApiResult(newsAbbrVoList);
    }

    /**
     * 获取所有工程
     * @param request
     * @return
     */
    @RequestMapping(value = "/project",method = RequestMethod.GET)
    @ResponseBody
    public ApiResult readProject(HttpServletRequest request){
        Long beamId = super.getBeamId(request);
        String rBeamId = request.getParameter("beamId");
        if(StringUtils.isNotEmpty(rBeamId) && NumberUtils.isDigits(rBeamId)){
            beamId = Long.parseLong(rBeamId);
        }
        List<ProjectVo> projectVoList = projectService.selectProject(beamId);
        return new ApiResult(projectVoList);
    }

    /**
     * 获取梁场信息
     * @param request
     * @return
     */
    @RequestMapping(value = "/getBeamContent",method = RequestMethod.GET)
    @ResponseBody
    public ApiResult getBeamContent(HttpServletRequest request){
        Long beamId = super.getBeamId(request);
        String rBeamId = request.getParameter("beamId");
        if(StringUtils.isNotEmpty(rBeamId) && NumberUtils.isDigits(rBeamId)){
            beamId = Long.parseLong(rBeamId);
        }
        BeamContentWithBLOBs beamContentWithBLOBs = beamContentService.getBeamContentData(beamId);
        return new ApiResult(beamContentWithBLOBs);
    }

    /**
     * 获取新闻
     * @param request
     * @return
     */
    @RequestMapping(value = "/readNewsData",method = RequestMethod.GET)
    @ResponseBody
    public PageResult readNewsData(HttpServletRequest request){
        String offset = request.getParameter("offset");
        String limit = request.getParameter("limit");
        Long beamId = super.getBeamId(request);
        String rBeamId = request.getParameter("beamId");
        if(StringUtils.isNotEmpty(rBeamId) && NumberUtils.isDigits(rBeamId)){
            beamId = Long.parseLong(rBeamId);
        }
        String newsId = request.getParameter("news_id");
        String newsSectionId = request.getParameter("newsSectionId");
        String title = request.getParameter("title");
        List<NewsVo> newsList = newsService.getNewsData(offset, limit, beamId, newsId, title, newsSectionId);
        Long total = newsService.countNewsData(beamId, newsId, title,newsSectionId);
        Integer page = null;

        if(total == null || total==0 || StringUtils.isEmpty(offset)){
            page = 0;
        }else{
            page =  Integer.parseInt(offset)/Integer.parseInt(limit)+1;
        }
        return new PageResult(total, page, newsList);
    }

}
